package com.tnxst.phexamination.public_health_docking_interface;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.tnxst.phexamination.callback.AppCallBack;
import com.tnxst.phexamination.constant.HISConfig;
import com.tnxst.phexamination.dto.DiabetesFollowDTO;
import com.tnxst.phexamination.dto.DoctorDTO;
import com.tnxst.phexamination.dto.InhabitantDTO;
import com.tnxst.phexamination.utils.GsonUtils;
import com.tnxst.phexamination.utils.RequestUtils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 广西数据同步
 */
public class SyncInterface {

    /**
     * 获取医生列表数据
     * @return
     */
    public static void syncDoctorList(Context context, GlobalViewModel viewModel){
        RequestUtils.getEmpty(HISConfig.REMOTE_URL + "app/getDoctorDTOList", new AppCallBack(context) {
            @Override
            public void onResponse(String response, int id) {
                if (StringUtils.isEmpty(response)){
                    Toast.makeText(context,"同步的医生数据为空！",Toast.LENGTH_SHORT);
                    return;
                }

                List<DoctorDTO> doctorDTOS = GsonUtils.parserJsonToArrayBeans(response, DoctorDTO.class);

                Iterator<DoctorDTO> iterator = doctorDTOS.iterator();
                while (iterator.hasNext()){
                    DoctorDTO doctorDTO = iterator.next();
                    viewModel.getDoctorRepository().insert(doctorDTO);
                }
                Toast.makeText(context,"同步医生数据成功！",Toast.LENGTH_LONG);
            }
        });
    }


    /**
     * 获取病人列表数据
     * @return
     */
    public static void syncInhabitantList(Context context, GlobalViewModel viewModel){
        RequestUtils.getEmpty(RequestUtils.splicingUrl("app/getInhabitantDTOList"), new AppCallBack(context) {
            @Override
            public void onResponse(String response, int id) {
                if (StringUtils.isEmpty(response)){
                    Toast.makeText(context,"同步的病人数据为空！",Toast.LENGTH_SHORT);
                    return;
                }

                List<InhabitantDTO> inhabitantDTOs = GsonUtils.parserJsonToArrayBeans(response, InhabitantDTO.class);

                Iterator<InhabitantDTO> iterator = inhabitantDTOs.iterator();
                while (iterator.hasNext()){
                    InhabitantDTO inhabitantDTO = iterator.next();
                    viewModel.getInhabitantRepository().insert(inhabitantDTO);
                }
                Toast.makeText(context,"同步病人数据成功！",Toast.LENGTH_LONG);

            }
        });
    }



    /**
     * 新增糖尿病
     * @return
     */
    public static void syncAddFollowDiabetes(Context context, DiabetesFollowDTO diabetesFollowDTO){

        Map<String, Object> data = new HashMap<>();
        data.put("inhabitantId",diabetesFollowDTO.getPersonID());
        data.put("inhabitantName",diabetesFollowDTO.getPersonName());
        data.put("followTime",diabetesFollowDTO.getFollowUpDate());
        data.put("followType",diabetesFollowDTO.getWayUp());
        data.put("symptom",diabetesFollowDTO.getSymptom());
        data.put("otherSymptom","");
        data.put("rightSbp",diabetesFollowDTO.getRightSbp());
        data.put("rightDbp",diabetesFollowDTO.getRightDbp());
        data.put("weight",diabetesFollowDTO.getWeight());
        data.put("height",diabetesFollowDTO.getHeight());
        data.put("bmi",diabetesFollowDTO.getBmi());
        data.put("dorsalisPedisArteryPulse",diabetesFollowDTO.getDorsalisPedisArteryPulse());
        data.put("dorsalisPulseResult",diabetesFollowDTO.getDorsalisPulseResult());
        data.put("examBodyOther",diabetesFollowDTO.getExamBodyOther());
        data.put("smoking",diabetesFollowDTO.getSmoking());
        data.put("dailyAlcoholIntake",diabetesFollowDTO.getDailyAlcoholIntake());
        data.put("exerciseWeekTimes",diabetesFollowDTO.getExerciseWeekTimes());
        data.put("eachExerciseTime",diabetesFollowDTO.getEachExerciseTime());
        data.put("food",diabetesFollowDTO.getStaple());
        data.put("psychology",diabetesFollowDTO.getPsychologicalAdjustment());
        data.put("complianceBehavior",diabetesFollowDTO.getComplianceBehavior());
        data.put("otherLaboratory",diabetesFollowDTO.getLabora());
        data.put("medicationCompliance",diabetesFollowDTO.getMedicationCompliance());
        data.put("adverseDrugReactions",diabetesFollowDTO.getAdverseDrugReactions());
        data.put("lowBloodSugarReactions",diabetesFollowDTO.getLowBloodSugarReactions());
        data.put("fuClassification",diabetesFollowDTO.getFuClassification());
        data.put("followDrug",diabetesFollowDTO.getDrug());
        data.put("insulindrug",diabetesFollowDTO.getInsulindrug());
        data.put("tranoutReasons",diabetesFollowDTO.getTranoutReasons());
        data.put("nextFollowTime",diabetesFollowDTO.getNextFollowUpDate());
        data.put("doctorId",diabetesFollowDTO.getDoctorID());
        data.put("doctorName",diabetesFollowDTO.getDoctorName());
        data.put("tranoutType",diabetesFollowDTO.getTargetOrgName());
        data.put("isDelete",0);
        String json = GsonUtils.toJson(data);
        RequestUtils.postJson(RequestUtils.splicingUrl("/followDiabetes/add"), json, new AppCallBack(context) {
            @Override
            public void onResponse(String response, int id) {
                Log.d("123456", "onResponse: " + response);

            }
        });
    }


}
